﻿// <copyright file="Global.cs" company="not for commercial use">
// Copyright (c) 6/4/2012 All Right Reserved
// </copyright>
// <author>Dimitry Kaplin</author>
// <date>6/4/2012 0:43 </date>
// <summary>Global programm options</summary>

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace EthernetTools.Options
{
    public class Global
    {
        #region Constant And Fields

        public const int MajorVersion = 0;
        public const int MinorVersion = 1;
        public const int SubMinorVersion = 23;

        public const string RegistryPath = @"HKEY_CURRENT_USER\software\DvkEhternetToools";

        public const string HelpUrl = "http://code.google.com/p/ethernet-tools/wiki/Help";

        private const string ReadBufferSizeValueName = "ReadBufferSize";
        private const string AutoScrollLogValueName = "AutoScrollLog";
        private const string CurrentLogLevelValueName = "CurrentLogLevel";
        private const string ArchitectureValueName = "Architecture";
        private const string LoadPreviousACsOnStartValueName = "LoadPreviousACsOnStart";
        private const string DialTimeOutValueName = "DialTimeOut";
        private const string CurrentPadtMethodValueName = "CurrentPadtMethod";
        private const string PadoSpeedLimitValueName = "PadoSpeedLimit";

        private static uint readBufferSize = 1024;

        private static bool autoScrollLog = true;

        private static string architecture = "x86";

        private static bool loadPreviousACsOnStart = false;

        private static int padoSpeedLimit = 0;

        private static uint dialTimeOut = 20000;

        private static int currentPadtMethod = (int)PadtMethod.HostNone | (int)PadtMethod.ConcentratorNone;
        
        #endregion 
        
        #region Properties

        public static bool GlobalAbortReadingPackets
        {
            get;
            set;
        }

        public static uint ReadBufferSize
        {
            get
            {
                return readBufferSize;
            }
            set
            {
                readBufferSize = value;
                try
                {
                    Registry.SetValue(RegistryPath, ReadBufferSizeValueName, ReadBufferSize.ToString(), RegistryValueKind.String);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Can't save to registry: " + ex.Message);
                }
            }

        }

        public static uint DialTimeOut
        {
            get
            {
                return dialTimeOut;
            }
            set
            {
                dialTimeOut = value;
                try
                {
                    Registry.SetValue(RegistryPath, DialTimeOutValueName, DialTimeOut.ToString(), RegistryValueKind.String);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Can't save to registry: " + ex.Message);
                }
            }

        }

        public static bool AutoScrollLog
        {
            get
            {
                return autoScrollLog;
            }
            set
            {
                autoScrollLog = value;
                // TODO: saving to registy or ini or what
            }

        }

        public static LogLevel CurrentLogLevel
        {
            get;
            set;
        }

        public static int CurrentPadtMethod
        {
            get
            {
                return currentPadtMethod;
            }
            set
            {
                currentPadtMethod = value;
                try
                {
                    Registry.SetValue(RegistryPath, CurrentPadtMethodValueName, currentPadtMethod.ToString(), RegistryValueKind.String);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Can't save to registry: " + ex.Message);
                }
            }
        }

        public static string Architecture
        {
            get 
            {
                return architecture;
            }
            set
            {
                architecture = value;
                try
                {
                    Registry.SetValue(RegistryPath, ArchitectureValueName, Architecture, RegistryValueKind.String);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Can't save to registry: " + ex.Message);
                }
            }
        }

        public static bool LoadPreviousACsOnStart
        {
            get
            {
                return loadPreviousACsOnStart;
            }
            set
            {
                loadPreviousACsOnStart = value;
                try
                {

                    Registry.SetValue(RegistryPath, LoadPreviousACsOnStartValueName, LoadPreviousACsOnStart.ToString(), RegistryValueKind.String);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Can't save to registry: " + ex.Message);
                }

            }
        }

        public static int PadoSpeedLimit
        {
            get
            {
                return padoSpeedLimit;
            }
            set
            {
                padoSpeedLimit = value;
                try
                {
                    Registry.SetValue(RegistryPath, PadoSpeedLimitValueName, padoSpeedLimit.ToString(), RegistryValueKind.String);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Can't save to registry: " + ex.Message);
                }

            }
        }

        #endregion

        #region Public Methods

        public static void SaveGlobalOptions()
        {
            try
            {
                Registry.SetValue(RegistryPath, ReadBufferSizeValueName, ReadBufferSize.ToString(), RegistryValueKind.String);
                Registry.SetValue(RegistryPath, AutoScrollLogValueName, AutoScrollLog.ToString(), RegistryValueKind.String);
                Registry.SetValue(RegistryPath, CurrentLogLevelValueName, ((int)CurrentLogLevel).ToString(), RegistryValueKind.String);
                Registry.SetValue(RegistryPath, ArchitectureValueName, Architecture, RegistryValueKind.String);
                Registry.SetValue(RegistryPath, LoadPreviousACsOnStartValueName, LoadPreviousACsOnStart.ToString(), RegistryValueKind.String);
                Registry.SetValue(RegistryPath, DialTimeOutValueName, DialTimeOut.ToString(), RegistryValueKind.String);
                Registry.SetValue(RegistryPath, CurrentPadtMethodValueName, ((int)currentPadtMethod).ToString(), RegistryValueKind.String);
                Registry.SetValue(RegistryPath, PadoSpeedLimitValueName, padoSpeedLimit.ToString(), RegistryValueKind.String);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to save settings to registry: " + ex.Message);
            }
        }

        public static void LoadGlobalOptions()
        {
            try
            {
                Object regValue = null;
                regValue = Registry.GetValue(RegistryPath, ReadBufferSizeValueName, null);
                if (regValue != null)
                {
                    string s = regValue.ToString();
                    uint.TryParse(s, out readBufferSize);
                }
                regValue = Registry.GetValue(RegistryPath, AutoScrollLogValueName, null);
                if (regValue != null)
                {
                    string s = regValue.ToString();
                    bool.TryParse(s, out autoScrollLog);
                }
                regValue = Registry.GetValue(RegistryPath, CurrentLogLevelValueName, null);
                if (regValue != null)
                {
                    string s = regValue.ToString();
                    int t = 0;
                    int.TryParse(s, out t);
                    CurrentLogLevel = (LogLevel)t;
                }
                regValue = Registry.GetValue(RegistryPath, ArchitectureValueName, null);
                if (regValue != null)
                {
                    architecture = regValue.ToString();
                }
                regValue = Registry.GetValue(RegistryPath, LoadPreviousACsOnStartValueName, null);
                if (regValue != null)
                {
                    string s = regValue.ToString();
                    bool.TryParse(s, out loadPreviousACsOnStart);
                }
                regValue = Registry.GetValue(RegistryPath, DialTimeOutValueName, null);
                if (regValue != null)
                {
                    string s = regValue.ToString();
                    uint.TryParse(s, out dialTimeOut);
                }
                regValue = Registry.GetValue(RegistryPath, CurrentPadtMethodValueName, null);
                if (regValue != null)
                {
                    string s = regValue.ToString();
                    int.TryParse(s, out currentPadtMethod);
                }
                regValue = Registry.GetValue(RegistryPath, PadoSpeedLimitValueName, null);
                if (regValue != null)
                {
                    string s = regValue.ToString();
                    int.TryParse(s, out padoSpeedLimit);
                }
            }
            catch(System.Exception ex)
            {
                MessageBox.Show("Failed to load settings from registry: " + ex.Message);
            }
        }

        #endregion

        public enum LogLevel
        {
            Debug = 0,
            Info = 1,
            Warning = 2,
            Error = 3,
            Assert = 4
        }

        [Flags]
        public enum PadtMethod
        {
            HostNone = 1,

            HostBroadcast = 1 << 2,

            HostDirect = 1 << 3,

            ConcentratorBroadcat = 1 << 4,

            ConcentratorDirect = 1 << 5,

            ConcentratorNone = 1 << 6,

        }
    }
}
